<template>
  <div class="analysis-page" style="font-size: initial">
    <section class="container">
      <SideTree :tempList="tempList" />
      <div class="main">
        <!--  -->
        <!-- 图表模型 -->
        <ModeIndex />
      </div>
      <!-- 图表配置 -->
      <GraphConfig />
    </section>

    <!-- 导出报告 -->
    <ExportReport ref="exportReport" :tempList="tempList" />
  </div>
</template>

<script>
import { mapState } from "vuex";
import SideTree from "./SideTree";
import GraphConfig from "./GraphConfig";
import ExportReport from "./ExportReport";
import base64 from "@/utils/base64.js";
// 图表模型
import ModeIndex from "./mode/index";
// 申请人（发明人）分析
// 申请量趋势分析
// import ApplicationTrend from './applicant/ApplicationTrend'

export default {
  components: {
    SideTree,
    GraphConfig,
    ExportReport,
    ModeIndex,
  },
  data() {
    return {
      ctNavID: "",
      routeType: "",
      searchTxt: "",
      tempList: [],
      all_total: 0,
      parmAggs: {},
      params: {},
      aggs: {},
      tempId: "",
      chartTempl: {},
    };
  },
  watch: {
    $route: {
      handler(nv) {
        this.tempId = nv.query.tempId;
        this.ctNavID = this.$route.query.dbid;
        //从地址中获取四个参数 1 dbid 2 eSearch 3 token 4 分面数据 templateId ==》 找出aggs 将整个数组作为入参
        this.eSearch = this.$route.query.eSearch
          ? base64.decode(this.$route.query.eSearch)
          : "";
        this.token = this.$route.query.token;
        // 存token
        this.$cookie.set("token", this.token);
        // this.tempId = this.$route.query.tempId;
        this.getTemplate();
      },
      immediate: true,
      deep: true
    }
  },

  created() {
    // this.ctNavID = this.$route.query.dbid;
    // //从地址中获取四个参数 1 dbid 2 eSearch 3 token 4 分面数据 templateId ==》 找出aggs 将整个数组作为入参
    // this.eSearch = this.$route.query.eSearch
    //   ? base64.decode(this.$route.query.eSearch)
    //   : "";
    // this.token = this.$route.query.token;
    // // 存token
    // this.$cookie.set("token", this.token);
    // this.tempId = this.$route.query.tempId;
    // this.getTemplate();
  },
  mounted() {},
  methods: {
    // 从数据库获取模板
    getTemplate() {
      console.log(3);
      this.$get(this.$Url.analysis.template + "?dbid=" + this.$route.query.dbid)
        .then((res) => {
          this.tempList = res.data;
          this.findAAggs();
        })
        .catch((err) => {});
    },
    findAAggs() {
      this.tempList.map((item) => {
        if (item.id == this.tempId) {
          this.aggs = item.tdata.param.aggs;
          this.chartTempl = item;
          this.search();
        } else {
          item.children &&
            item.children.map((it) => {
              if (it.id == this.tempId) {
                this.aggs = it.tdata.param.aggs;
                this.chartTempl = it;
                this.search();
              }
            });
        }
      });
    },
    //搜索事件
    search(ops = { str: "", ipi: "", filter: {} }) {
      let startTime = localStorage.getItem("chooseStartTime") || 2013;
      let endTime = localStorage.getItem("chooseEndTime") || 2023;
      let datefield = this.chartTempl.tdata.datefield;
      let datestr = this.ctNavID == "paper" ? ` AND ${datefield} >= ${startTime} AND ${datefield} <= ${endTime}` : ` AND ${datefield} >= ${startTime}-01-01 AND ${datefield} <= ${endTime}-01-01`
      
      let url = "" 
      console.log("this.tempId",this.tempId);
      switch(this.tempId){
        case "101000":
          url = this.$Url.search.patentAggsTrends ; // 图表分析单独处理 - 专利趋势分析
          break;
        case "102000":
          url = this.$Url.search.antypeTrends ; // 图表分析单独处理 - 法律状态趋势
          break;
        case "104000":
          url = this.$Url.search.legaltrendsTrends ;// 图表分析单独处理 - 申请人类别分析
          break;
        case "105000":
          url = this.$Url.search.countryTrends ;  // 图表分析单独处理 - 国内外趋势分析
          break;
        case "601000":
          url = this.$Url.search.anpartnershipTrends ; // 图表分析单独处理 - 合作关系分析
          break;
        default:
          url = this.$Url.search.commonSearch ;
          break;
      }
      this.params = {
        // eSearch: this.eSearch + `AND ${datefield}>=2013-01-01 AND ${datefield}<=2023-01-01` ,
        /**
         * 具体参考系统个人中心-帮助文档模块
         * 以下为距离：
         * 如果需要在报告中限定分析日期范围 则需要在检索条件中增加 日期参数 如下：
         * 专利：无人机 AND isd >=2013-01-01 AND isd <=2023-01-01  论文：year >=2013 AND year<=2023
         * 或者如下：
         * 专利：无人机 AND isd/20130101
         *
         * 解释：isd为公开日纬度，也可以切换其他维度。
         * **/
        eSearch: this.eSearch + datestr,
        dbid: this.ctNavID,
        ipi: 1,
        ips: this.tempId == 101000 ? 1 : 0, //用于区分当datatype为15时，接口返回特定格式数据
        sort: [],
        aggs: this.aggs,
        rSearch: "",
        smartSearch: "",
      };
      // 耗时
      this.$post(url, this.params)
        .then((res) => {
          let data = res.data;
          data["chartTempl"] = this.chartTempl;
          data["datatype"] = this.chartTempl.tdata.datatype;
          this.$Bus.$emit("dcData", data);
        })
        .catch((err) => {
          // this.emptyText = "无法找到你所查询的内容，换个关键词试试";
        });
    },
  },
  beforeDestroy() {},
};
</script>

<style lang="scss" scoped>
::v-deep {
  .el-button--primary {
    border-color: $color141 !important;
    background-color: $color141 !important;
  }
  .el-checkbox__label {
    font-size: 12px !important;
    color: $color32;
  }
  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #000 !important;
  }
  .el-dropdown-menu__item {
    font-size: 12px !important;
  }
  .el-select-dropdown__item.selected {
    color: $color141 !important;
  }
  .el-pager li.active {
    color: $color141 !important;
  }
  // 选择数据源
  .el-dialog__header {
    background-color: $color4;
  }
  .el-dialog__body {
    // padding: 0 20px !important;
  }
  .group-title-cnt .el-checkbox__label {
    font-size: 14px;
    font-weight: 700;
    color: #000 !important;
  }
  .group-world {
    .el-checkbox__input {
      line-height: normal;
    }
    .el-checkbox__label {
      font-size: 12px;
      color: #000 !important;
    }
  }
  // 筛选项
  .tree-items {
    .el-checkbox__label {
      padding-left: 5px;
      font-size: 12px !important;
    }
  }
  .mode-select,
  .sort-select {
    .el-input__inner {
      height: 34px;
      line-height: 34px;
      border: none;
      color: $color30;
    }
  }
  // 分页
  .pagination .page-size .el-input__inner {
    height: 28px;
    line-height: 28px;
  }
  .el-select-dropdown__item {
    text-indent: 10px;
  }

  // 参数配置
  .search-time {
    // .el-date-editor.el-input {
    //   width: 142px;
    //   .el-input__inner {
    //     border: 0;
    //     background-color: $color2;
    //   }
    // }
  }
  // 分析页面
  .m-slot {
    &.no-margin {
      margin-bottom: 0 !important;
    }
    .btn-group {
      .icon {
        font-size: 18px !important;
      }
    }
    h4 {
      line-height: 40px;
      font-size: 13px;
      font-weight: normal;
      color: $color130;
    }
    .date-box {
      position: relative;
      display: inline-block;
      margin-right: 20px;
      font-size: 13px;
      .tit {
        display: inline-block;
        padding-right: 14px;
        font-size: 12px;
        color: $color6;
      }
      .search-time {
        display: inline-flex;
        align-items: center;
        .split {
          position: relative;
          display: inline-block;
          width: 10px;
          margin: 0 10px;
          &:before {
            position: absolute;
            top: 50%;
            left: 0;
            width: 100%;
            height: 1px;
            background-color: $color31;
            content: "";
          }
        }
      }
    }
    .select-box {
      position: relative;
      display: inline-block;
      margin-right: 20px;
      font-size: 13px;
      .tit {
        display: inline-block;
        padding-right: 14px;
        font-size: 12px;
        color: $color6;
      }
      .select {
        width: 142px;
        .el-input__inner {
          padding-left: 15px;
          border: 0;
          font-size: 12px;
          background-color: $color4;
        }
        // .el-input__inner {
        //   border: 0;
        //   background-color: $color2;
        // }
      }
    }
    .right-tools {
      position: absolute;
      top: 0;
      right: 0;
      // width: 176px;
      padding-right: 10px;
      text-align: right;
      > span {
        display: inline-block;
        width: 78px;
        line-height: 26px;
        border: 1px solid $mainColor;
        border-radius: 30px;
        margin-left: 10px;
        text-align: center;
        font-size: 12px;
        color: $color2;
        background-color: $mainColor !important;
        cursor: pointer;
        &:nth-of-type(1) {
          margin-left: 0;
        }
      }
    }
  }

  // 分析-图表
  .chart-box {
    position: relative;
    width: 100%;
    .chart {
      width: 100%;
      height: calc(100vh - 0.7rem - 40px - 40px - 70px - 41px - 70px);
    }
    .chart-loading-box {
      position: absolute;
      top: 50%;
      left: 0;
      width: 100%;
      margin-top: -5em;
      max-height: 100%;
    }
  }
  .picker-box {
    position: relative;
    display: inline-block;
    .el-date-picker {
      position: relative;
      width: 142px !important;
      height: 30px;
      .el-input__inner {
        padding-left: 15px;
        border: 0;
        font-size: 12px;
        background-color: $color4;
      }
    }
    .icon-riqi {
      position: absolute;
      top: 0;
      right: 10px;
      line-height: 32px;
      font-size: 21px;
      color: $mainColor;
    }
  }

  // 分析页面的表格
  .chart-tab {
    width: 100%;
    overflow-x: auto;
    .chart-tab-ul {
      position: relative;
      min-width: max-content;
      li {
        position: relative;
        display: flex;
        width: 100%;
        padding: 8px 0;
        border-bottom: 1px solid $color162;
        font-size: 12px;
        color: $color143;
        &.tit {
          background-color: $color154;
          p {
            font-size: 12px;
            font-weight: bold;
            color: $color10;
          }
        }
        &.content {
          &:nth-of-type(2n-1) {
            background-color: $color161;
          }
          &:nth-of-type(2n) {
            background-color: #fff;
          }
          &:hover {
            &:before {
              position: absolute;
              top: 0;
              left: 0;
              width: 3px;
              height: 100%;
              background-color: $color164;
              content: "";
            }
            background-color: $color163 !important;
          }
          p {
            &:first-of-type {
              color: $color6;
            }
          }
        }
        p {
          flex: 1;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          min-width: 75px;
          line-height: 23px;
          padding: 0 10px;
          text-align: center;
        }
      }
    }
  }
}
.analysis-page {
  position: relative;
  width: 100%;
  min-height: calc(100vh - 1rem);
  background-color: $color152;
}
.container {
  display: flex;
  flex-wrap: wrap;
  min-width: 1240px;
  .main {
    flex: 1;
    width: 100%;
    min-width: 0; // 重点
    padding: 20px 20px 0;

    > .tools {
      position: relative;
      top: -30px;
      display: inline-flex;
      align-items: center;
      justify-content: flex-end;
      width: 100%;
      margin-top: 10px;
      margin-bottom: -10px;
      button {
        width: 100px;
        height: 30px;
        border-color: $mainColor;
        border-radius: 15px;
        font-size: 12px;
        color: $color51;
        background-color: $mainColor;
        cursor: pointer;
        i {
          margin-right: 2px;
        }
      }
      .download-center-btn {
        font-size: 14px;
        margin-left: 10px;
        text-decoration: underline;
        color: #242424;
        cursor: pointer;
        &:hover {
          color: $mainColor;
        }
      }
    }
  }
}
// animate
.slide-fade-enter-active {
  transition: all 0.2s ease;
}
.slide-fade-enter {
  margin-right: -256px;
  opacity: 0;
}
</style>
